﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Data.SqlClient;
using System.Data;
using Utils;

namespace AlertSystem.Business
{
    public partial class Warning_Log
    {
        static DBDataContext context = new DBDataContext(AppSetting.ConnectionString);

        public static void InsertLog(Exception ex)
        {
            InsertLog(ex.Message, ex.StackTrace, ex.Source);
        }

        public static void InsertLog(string logMessage, string logTrace, string logSource)
        {
            try
            {
                Warning_Log log = new Warning_Log();
                log.LogSource = logSource;
                log.LogMessage = logMessage;
                log.LogTime = DateTime.Now;
                log.LogTrace = logTrace;
                context.Warning_Logs.InsertOnSubmit(log);
                context.SubmitChanges();
            }
            catch { }
        }

        public static List<Warning_Log> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord, DateTime? fromTime, DateTime? toTime)
        {
            var result = context.Warning_Logs.OrderByDescending(p => p.LogID).AsQueryable();
            if (fromTime != null)
                result = result.Where(p => p.LogTime >= fromTime);
            if (toTime != null)
                result = result.Where(p => p.LogTime < toTime.Value.AddDays(1));
            totalRecord = result.Count();
            return result
                    .Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize)
                    .Take(pageSize)
                    .ToList();
        }
    }
}
